package br.com.hbt.general.files;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class FileTests {

    private File mainDir;
    private File mainFile;

    public void writeStuffFile() {
        try {
            FileWriter fw = new FileWriter(mainFile);
            PrintWriter pw = new PrintWriter(fw);
            pw.println("Teste de escrever linha 1");
            pw.println("Teste de escrever linha 2");
            pw.flush();

        } catch (FileNotFoundException e) {
            System.out.println("Não achei o arquivo");
        } catch (IOException e) {
            System.out.println("Não consegui escrever nada no arquivo");
        }

    }

    public void loadDirectory() {
        setMainDir(new File("/home/herbert/desenv/arquivos"));
        if (!getMainDir().exists()) {
            getMainDir().mkdir();
        } else {
            System.out.println("Diretório já existe e tá pronto pra uso!");
        }
    }

    public void loadFile() throws IOException {

        loadDirectory();

        if (getMainDir().exists()) {
            setMainFile(new File(getMainDir(), "teste.txt"));
            if (!getMainFile().exists()) {
                System.out.println("Arquivo Criado" + getMainFile());
            } else {
                System.out.println("Arquivo já existe e tá disponivel pra uso!");
            }
        } else {
            System.out.println("Pau no Diretório!");
        }

    }

    public static void main(String[] args) throws IOException {
        FileTests ft = new FileTests();
        ft.loadFile();
        System.out.println(ft.getMainFile().getAbsolutePath());

        ft.writeStuffFile();

    }

    public File getMainFile() {
        return mainFile;
    }

    public void setMainFile(File mainFile) {
        this.mainFile = mainFile;
    }

    public File getMainDir() {
        return mainDir;
    }

    public void setMainDir(File mainDir) {
        this.mainDir = mainDir;
    }
}
